package com.manxi.mapper;

import com.github.pagehelper.Page;
import com.manxi.annotation.AutoFill;
import com.manxi.dto.DishPageQueryDTO;
import com.manxi.entity.Dish;
import com.manxi.enumeration.OperationType;
import com.manxi.vo.DishVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DishMapper {

    /**
     * 根据分类id查询菜品数量
     *
     * @param categoryId
     * @return
     */
    @Select("select count(id) from dish where category_id = #{categoryId}")
    Integer countByCategoryId(Long categoryId);

    /**
     * 新增菜品
     *
     * @param dish
     */
    @Insert("insert into dish (name,category_id,count,price,image,description,status,create_time,update_time,create_user,update_user) values (#{name},#{categoryId},#{count},#{price},#{image},#{description},#{status},#{createTime},#{updateTime},#{createUser},#{updateUser})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @AutoFill(value = OperationType.INSERT)
    void insert(Dish dish);

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Select("<script> " +
            "select d.* , c.name as categoryName from dish d left outer join category c on d.category_id = c.id " +
            "where 1 = 1 " +
            "<if test='name != null'>AND d.name like CONCAT('%',#{name},'%') </if> " +
            "<if test='categoryId != null'>AND d.category_id = #{categoryId} </if> " +
            "<if test='status != null'>AND d.status = #{status} </if> " +
            "order by d.create_time desc " +
            "</script>")
    Page<DishVO> pageQuery(DishPageQueryDTO dishPageQueryDTO);

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @Select("select * from dish where id = #{id}")
    Dish getById(Long id);


    /**
     * 根据id删除菜品
     *
     * @param id
     */
    @Delete("delete from dish where id = #{id}")
    void deleteById(Long id);

    /**
     * 更改菜品信息
     *
     * @param dish
     */
    @Update("<script> " +
            "update dish set " +
            "<if test='name != null'> name =#{name}, </if>" +
            "<if test='categoryId != null'> category_id =#{categoryId}, </if>" +
            "<if test='price != null'> price =#{price}, </if>" +
            "<if test='image != null'> image =#{image}, </if>" +
            "<if test='description != null'> description =#{description}, </if>" +
            "<if test='updateTime != null'> update_time =#{updateTime}, </if>" +
            "<if test='updateUser != null'> update_user =#{updateUser}, </if>" +
            "<if test='count != null'> count =#{count} </if>" +
            "<if test='status != null'> status =#{status} </if>" +
            "where id = #{id} " +
            "</script>")
    @AutoFill(value = OperationType.UPDATE)
    void update(Dish dish);


    /**
     * 套餐查询菜品列表
     *
     * @param dish
     * @return
     */
    @Select("<script> " +
            "select * from dish " +
            "where 1 = 1 " +
            "<if test = 'name != null'> AND name like CONCAT('%',#{name},'%') </if> " +
            "<if test = 'status != null'> AND status = #{status} </if> " +
            "<if test = 'categoryId != null'> AND category_id = #{categoryId} </if> " +
            "order by create_time desc " +
            "</script>")
    List<Dish> list(Dish dish);

    /**
     * 查询菜品状态数量
     * @param status
     * @return
     */
    @Select("select count(id) from dish where status = #{status}")
    Integer countStatus(Integer status);

}
